var JS_ADOWNER = function(){
	var owner = this;
	owner.dom_tag = "adowner";
	owner.dom_search = $("#"+owner.dom_tag+"_div_search");
	owner.dom_edit = $("#"+owner.dom_tag+"_div_add_edit");
	owner.dom_table = $("#"+owner.dom_tag+"_table");
	owner.currentId = null;
	owner.currentRow = 0;
	owner.sortName = "addTime";
	owner.sortOrder = false;
	owner.map_data = {};
	owner.isShowSearchWindow = false;
	
	owner.dom_table.datagrid({
		onSortColumn: function(sort, order){
			owner.sortName = sort;
			owner.sortOrder = true?(order=="asc"):false;
			owner.getPage(1);
		}
	}).datagrid("getPager").pagination({
	    layout: ["list","sep","first","prev","sep","manual","sep","next","last","refresh","sep"],
	    pageList: [10,20,50,100],
	    pageSize: 20,
	    buttons: [{
	        iconCls:"icon-search",
	        handler:function(){
	            owner.dom_search.window("open").window("hcenter");
	            if(!owner.isShowSearchWindow) {
	            	$("#ipt_"+owner.dom_tag+"_search_begin").Zebra_DatePicker({
						pair: $("#ipt_"+owner.dom_tag+"_search_end")
					});
					$("#ipt_"+owner.dom_tag+"_search_end").Zebra_DatePicker({
						direction: true
					});
					owner.isShowSearchWindow = true;
	            }
	        }
	    },{
	        iconCls:"icon-edit",
	        handler:function(){
	        	if(owner.dom_table.datagrid("getChecked").length!=1){
	        		$.messager.alert("提示信息", "必须仅选择一条数据进行编辑！");
	        		return;
	        	}
	        	var row = owner.dom_table.datagrid("getChecked")[0];
	        	owner.currentId = row.itemId;
	        	owner.currentRow = owner.dom_table.datagrid("getRowIndex", row);
	            owner.dom_edit.window({
	            	"iconCls": "icon-edit",
	            	"title": "编辑广告业主"
	            }).window("open").window("hcenter");
	            owner.resetSaveOrEdit();
	        }
	    },{
	        iconCls:"icon-add",
	        handler:function(){
	            owner.currentId = null;
	            owner.dom_edit.window({
	            	"iconCls": "icon-add",
	            	"title": "添加广告业主"
	            }).window("open").window("hcenter");
	            owner.resetSaveOrEdit();
	        }
	    },{
	        iconCls:"icon-remove",
	        handler:function(){
	        	var rows = owner.dom_table.datagrid("getChecked");
	            if(rows.length<1){
	        		$.messager.alert("提示信息", "必须选择至少一条数据！");
	        		return;
	        	}
	        	$.messager.confirm("提示信息","确定删除这些数据?",function(isDel){
				    if(isDel) {
				    	var ids = new Array();
				    	for (var i=0; i < rows.length; i++) {
						  ids.push(rows[i].itemId);
						};
						$.ajax({
							type : "post",
							url : "/manager/business/adowner/del",
							cache : false,
							dataType : "json",
							data: {
								"adownerIds": ids
							}
						}).done(function(result){
							if(result.isTrue) {
								for (var i=0; i < rows.length; i++) {
									owner.dom_table.datagrid("deleteRow", owner.dom_table.datagrid("getRowIndex", rows[i]));
								};
							} else {
								$.messager.alert("提示信息", result.message);
							}
						}).fail(function(){
							$.messager.alert("提示信息", "网络繁忙，请稍后再试！");
						});
				    }
				});
	        }
	    }],
	    onSelectPage: function(pageNum, pageSize){
	    	owner.getPage(pageNum, pageSize);
	    }
	}); 
	
	owner.init = function(){
		if(window.localStorage.getItem("adminRoleName")!="超级管理员") {
			$("#ipt_"+owner.dom_tag+"_search_company").parent().hide();
			owner.dom_table.datagrid("hideColumn", "area");
			owner.dom_table.datagrid("hideColumn", "company");
		}
	};
	
	owner.resetSearch = function(){
		owner.dom_search.find(".easyui-textbox").textbox("clear")
		.end().find(".easyui-combobox").combobox("clear")
		.end().find(".easyui-combotree").combotree("clear")
		.end().find(".easyui-numberbox").numberbox("clear")
		.end().find(".textbox").val("");
	};
	
	owner.formatItem = function(obj){
		var item = {
			"itemId": obj.ownerId,
			"name": obj.ownerName,
			"area": obj.areaName,
			"company": obj.companyName,
			"phone": obj.ownerPhone1,
			"cellphone": obj.ownerPhone2,
			"fax": obj.ownerFax,
			"email": obj.ownerEmail,
			"address": obj.ownerAddress,
			"zipcode": obj.ownerZipcode,
			"desc": obj.ownerDesc,
			"addTime": obj.addTime.date(),
			"updateTime": obj.updateTime.date()
		};
		return item;
	};
	
	owner.getPage = function(pageNum, pageSize){
		if(pageSize==null) {
			pageSize = owner.dom_table.datagrid("getPager").pagination("options").pageSize;
		}
		var params = {};
		if(!owner.isShowSearchWindow) {
			params = {
					"searchCompany": "", 
					"searchName": "", 
					"searchPhone": "", 
					"searchCellphone": "",
					"searchBegin": "",
					"searchEnd": "",
					"pageNum": pageNum,
					"pageSize": pageSize,
					"orderName": owner.sortName,
					"orderFlag": owner.sortOrder
			};
		} else {
			params = {
					"searchCompany": $("#ipt_"+owner.dom_tag+"_search_company").textbox("getValue"), 
					"searchName": $("#ipt_"+owner.dom_tag+"_search_name").textbox("getValue"), 
					"searchPhone": $("#ipt_"+owner.dom_tag+"_search_phone").textbox("getValue"), 
					"searchCellphone": $("#ipt_"+owner.dom_tag+"_search_cellphone").textbox("getValue"), 
					"searchBegin": $("#ipt_"+owner.dom_tag+"_search_begin").val(),
					"searchEnd": $("#ipt_"+owner.dom_tag+"_search_end").val(),
					"pageNum": pageNum,
					"pageSize": pageSize,
					"orderName": owner.sortName,
					"orderFlag": owner.sortOrder
			};
		}
		$.ajax({
			type : "get",
			url : "/manager/business/adowner/list",
			cache : false,
			dataType : "json",
			data: params
		}).done(function(result){
			if(result.isTrue) {
				var ps = result.ps;
				var data = new Array();
				owner.map_data = {};
				if(ps.items.length==0) {
					$.messager.alert("提示信息", "暂无数据！");
					return;
				}
				for(var i=0; i<ps.items.length; i++) {
					var obj = ps.items[i];
					owner.map_data[obj.ownerId] = obj;
					data.push(owner.formatItem(obj));
				}
				owner.dom_table.datagrid("loadData", {
					"total": ps.totalCount,
					"rows": data
				}); 
			} else {
				$.messager.alert("提示信息", result.message);
			}
		}).fail(function(){
			$.messager.alert("提示信息", "网络繁忙，请稍后再试！");
		});
	};
	
	owner.resetSaveOrEdit = function(){
		if(owner.currentId==null) {
			owner.dom_edit.find(".easyui-textbox").textbox("clear")
			.end().find(".easyui-combobox").combobox("clear")
			.end().find(".easyui-combotree").combotree("clear")
			.end().find(".easyui-numberbox").numberbox("clear")
			.end().find(".textbox").val("");
			$("#"+owner.dom_tag+"_ipt_pwd").textbox({
				required: true
			});
		} else {
			var item = owner.map_data[owner.currentId];
			$("#"+owner.dom_tag+"_ipt_name").textbox("setValue", item.ownerName);
			$("#"+owner.dom_tag+"_ipt_pwd").textbox({
				required: false
			}).textbox("clear");
			$("#"+owner.dom_tag+"_ipt_phone1").textbox("setValue", item.ownerPhone1);
			$("#"+owner.dom_tag+"_ipt_phone2").textbox("setValue", item.ownerPhone2);
			$("#"+owner.dom_tag+"_ipt_fax").textbox("setValue", item.ownerFax);
			$("#"+owner.dom_tag+"_ipt_email").textbox("setValue", item.ownerEmail);
			$("#"+owner.dom_tag+"_ipt_address").textbox("setValue", item.ownerAddress);
			$("#"+owner.dom_tag+"_ipt_zipcode").textbox("setValue", item.ownerZipcode);
			$("#"+owner.dom_tag+"_ipt_desc").textbox("setValue", item.ownerDesc);
		}
	};
	
	owner.saveOrEdit = function(dom){
		dom.linkbutton("disable");
		var isAllValid = new Array();
		owner.dom_edit.find(".easyui-textbox").each(function(){
			if(!$(this).textbox("isValid")){
				isAllValid.push($(this).prev("label").html());
			}
		});
		if(isAllValid.length>0){
			$.messager.alert("提示信息", isAllValid.join(",")+" 输入不正确！");
			dom.linkbutton("enable");
			return;
		}
		var name = $("#"+owner.dom_tag+"_ipt_name").textbox("getValue");
		var pwd = $("#"+owner.dom_tag+"_ipt_pwd").textbox("getValue");
		var phone = $("#"+owner.dom_tag+"_ipt_phone1").textbox("getValue");
		var cellphone = $("#"+owner.dom_tag+"_ipt_phone2").textbox("getValue");
		var fax = $("#"+owner.dom_tag+"_ipt_fax").textbox("getValue");
		var email = $("#"+owner.dom_tag+"_ipt_email").textbox("getValue");
		var address = $("#"+owner.dom_tag+"_ipt_address").textbox("getValue");
		var zipcode = $("#"+owner.dom_tag+"_ipt_zipcode").textbox("getValue");
		var desc = $("#"+owner.dom_tag+"_ipt_desc").textbox("getValue");
		$.ajax({
			type : "post",
			url : "/manager/business/adowner/addoredit",
			cache : false,
			dataType : "json",
			data: {
				"ownerId": owner.currentId,
				"ownerName": name, 
				"ownerPwd": pwd, 
				"ownerPhone1": phone, 
				"ownerPhone2": cellphone, 
				"ownerFax": fax, 
				"ownerEmail": email, 
				"ownerAddress": address, 
				"ownerZipcode": zipcode, 
				"ownerDesc": desc
			}
		}).done(function(result){
			if(result.isTrue) {
				var item = result.tag;
				if(owner.currentId==null) {
					owner.dom_table.datagrid("appendRow", owner.formatItem(item));
				} else {
					owner.dom_table.datagrid("updateRow", {
						index: owner.currentRow,
						row: owner.formatItem(item)
					});
				}
				owner.map_data[item.ownerId] = item;
				owner.dom_edit.window("close");
			} else {
				$.messager.alert("提示信息", result.message);
			}
		}).fail(function(){
			$.messager.alert("提示信息", "网络繁忙，请稍后再试！");
		}).complete(function(){
			dom.linkbutton("enable");
		});
	};
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
};